"""
requests:网络爬虫
"""
import requests, re

head = {"User-Agent": "Moozilla/5.0 (Windows NT 10.0; Win64; x64)"}
url = "http://www.weather.com.cn/weather1d/101010100.shtml"
resp = requests.get(url,headers=head)
resp.encoding = "utf-8"
if resp.ok:
    # print(resp.text)  # 得到html页面，文本形式

    # ['景区', '三亚', '九寨沟', '大理', '张家界', '桂林', '青岛']
    # ['天气', '多云', '多云', '小雨转多云', '多云', '中雨转暴雨', '多云']
    # ['气温', '35/26℃', '28/16℃', '29/17℃', '35/24℃', '28/24℃', '24/19℃']
    # ['旅游指数', '一般', '适宜', '适宜', '一般', '一般', '适宜']
    city_list = re.findall('<span class="name">([\u4e00-\u9fa5]+)</span>', resp.text)
    weather_list = re.findall('<span class="weather">([\u4e00-\u9fa5]+)</span>', resp.text)
    wd_list = re.findall('<span class="wd">(.+)</span>', resp.text)
    zs_list = re.findall('<span class="zs">([\u4e00-\u9fa5]+)</span>', resp.text)

    # 数据打包
    zip_list = []
    for a, b, c, d in zip(city_list, weather_list, wd_list, zs_list):
        # zip_list.append({a:[b,c,d]})
        zip_list.append([a, b, c, d])
    for i in zip_list:
        print(i)

    url2 = 'https://i.weather.com.cn/images/mobile/mtqxw/2024/06/04/20240604152550045C9F07FB66DCBDCDC8A5F4C8334859.jpg'
    resp2 = requests.get(url2)
    resp2.encoding = "utf-8"
    print(resp2.content)  # 得到图片，二进制形式
    with open("tupian.png", "wb") as f:
        f.write(resp2.content)
else:
    print("获取失败")
